package com.jifeng.api.utils;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ReUtil;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtils {

    /**
     * 判断订单是否超时
     */

    /**
     * @param beginDate 开始租赁时间
     * @param endDate   结束租赁时间
     * @param baseHour  基础小时
     * @return 超时费用
     */
    public static int orderIsOvertime(Date beginDate, Date endDate, int baseHour, int extFee) {
        long useHour = DateUtil.between(
                beginDate,
                endDate,
                DateUnit.HOUR);
        int intUseHour = new Long(useHour).intValue();
        if (intUseHour == baseHour) {
            int beginMinute = DateUtil.minute(beginDate);
            int endMinute = DateUtil.minute(endDate);
            if (endMinute > beginMinute) {
                return extFee;
            } else if (beginMinute == endMinute) {
                int beginSecond = DateUtil.second(beginDate);
                int endSecond = DateUtil.second(endDate);
                if (endSecond >= beginSecond) {
                    return extFee;
                } else {
                    return 0;
                }
            } else {
                return 0;
            }
        } else if (intUseHour < baseHour) {
            return 0;
        } else {
            int beginMinute = DateUtil.minute(beginDate);
            int endMinute = DateUtil.minute(endDate);
            if (endMinute > beginMinute) {
                return (intUseHour - baseHour) * extFee + extFee;
            } else if (beginMinute == endMinute) {
                int beginSecond = DateUtil.second(beginDate);
                int endSecond = DateUtil.second(endDate);
                if (endSecond > beginSecond) {
                    return (intUseHour - baseHour) * extFee + extFee;
                } else if (endSecond == beginSecond) {
                    return (intUseHour - baseHour) * extFee;
                } else {
                    return (intUseHour - baseHour) * extFee + extFee;
                }
            } else {
                return (intUseHour - baseHour) * extFee + extFee;
            }
        }
    }

    /**
     * 校验机柜编号（terminal_id)(IOT)的格式
     * 规则：15位
     */
    public static boolean isIOT(String iot) {
        return ReUtil.isMatch("[0-9]{15}", iot);
    }

    /**
     * 判断是否是身份证格式
     *
     * @param idcard 身份证
     * @return boolean
     */
    public static boolean isIdCard(String idcard) {
        return IdcardUtil.isValidCard(idcard);
    }


    /**
     * 判断是否是最长4位汉字的名字
     *
     * @param name 名字
     * @return boolean
     */
    public static boolean isFourName(String name) {
        return ReUtil.isMatch("[\u4E00-\u9FA5]{1,4}", name);
    }

    /**
     * 判断是否是6位数字的密码
     *
     * @param password 密码
     * @return boolean
     */
    public static boolean isPassword(String password) {
        return ReUtil.isMatch("[0-9]{6}", password);
    }

    /**
     * 判断是否是手机号码
     *
     * @param account 手机号
     * @return boolean
     */
    public static boolean isPhone(String account) {
        return ReUtil.isMatch("1[0-9]{10}", account);
    }

    // 判断是否是全数字
    public static boolean isAllNumer(String str) {
        //Pattern pattern = Pattern.compile("^-?[0-9]+"); //这个也行
        Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");//这个也行
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    // 判断是否是10位数并且以规定的数字开头
    public static boolean isNumber(String str, int type) {
        try {
            Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");//这个也行
            Matcher isNum = pattern.matcher(str);
            if (!isNum.matches() || str.length() != 10) {
                return false;
            }
            if (type == 0) {
                return str.startsWith("0");
            } else if (type == 1) {
                return str.startsWith("1");
            } else if ((type == 2)) {
                return str.startsWith("2");
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
